package com.qmhl.toolposition.fragment.msg

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.hjq.toast.Toaster
import com.lxj.xpopup.XPopup
import com.qmhl.toolposition.R
import com.qmhl.toolposition.adapter.msg.FriendMsgAdapter
import com.qmhl.toolposition.api.UserService
import com.qmhl.toolposition.api.content.FriendMsg
import com.qmhl.toolposition.databinding.FragmentMsgFriendBinding
import com.qmhl.toolposition.event.TabSelectEvent
import com.qmhl.toolposition.utils.PreferenceUtil
import kotlinx.coroutines.launch
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode


/**
 * @author: lqhl
 * @createTime: 2023-11-04  23:17
 * @description: 好友消息
 */
class MsgFriendFragment : Fragment() {

    var num: Int = 1
    var size: Int = 10
    lateinit var adapter: FriendMsgAdapter

    private lateinit var binding: FragmentMsgFriendBinding
    private lateinit var msgList: List<FriendMsg>


    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
    ): View? {
        binding = FragmentMsgFriendBinding.inflate(inflater, container, false)
        initData()
        initListener()
        return binding.root
    }

    private fun initListener() {


    }

    private fun initData() {
        EventBus.getDefault().register(this)
        getDataFlushList()

        binding.refreshLayout.setOnRefreshListener { refresh ->
            num = 1
            getDataFlushList()
            refresh.finishRefresh(1000 /*,false*/) //传入false表示刷新失败
        }


        binding.refreshLayout.setOnLoadMoreListener { refresh ->
            num += 1
            getDataLoadMoreList()
            refresh.finishLoadMore(1000 /*,false*/) //传入false表示加载失败
        }


    }

    private fun getDataFlushList() {
        var currentUser = PreferenceUtil.getCurrentUser()
        if (!currentUser.isLogin) {
            return
        }
        lifecycleScope.launch {
            var content = UserService.create()
                .getFriendMsgList(num, size)
            if (content.code == 200) {
                val dataList = content.data
                adapter = FriendMsgAdapter(dataList)
                if (dataList.size > 0) {
                    binding.llContainer.visibility = View.INVISIBLE
                }
                binding.rv.adapter = adapter
                binding.rv.layoutManager = LinearLayoutManager(context)


                // 同意 的点击事件
                adapter.addOnItemChildClickListener(R.id.tv_agree) { adapter, view, position ->
                    showAgreeDialog(adapter.items[position])

                }

                // 拒绝 的点击事件
                adapter.addOnItemChildClickListener(R.id.tv_refuse) { adapter, view, position ->
                    showRefuseDialog(adapter.items[position])
                }
            }

        }

    }

    private fun showAgreeDialog(friendMsg: FriendMsg) {


        XPopup.Builder(context)
            .asConfirm(
                "温馨提示",
                "若您点击确定，即代表您同意平台收集和使用您地理位置相关信息，且同意成为对方好友。允许平台将位置轨迹等相关信息分享给对方查看。若您想停止分享，则可以删除对方好友"
            ) {
                agreeRequest(friendMsg)
            }
            .show()

    }

    private fun agreeRequest(friendMsg: FriendMsg) {
        lifecycleScope.launch {
            var content = UserService.create()
                .agree(friendMsg.id)
            if (content.code == 200) {
                getDataFlushList()
            }

        }
    }

    private fun refuseRequest(friendMsg: FriendMsg) {
        lifecycleScope.launch {
            var content = UserService.create()
                .refuse(friendMsg.id)
            if (content.code == 200) {
                getDataFlushList()
            }

        }
    }

    private fun showRefuseDialog(friendMsg: FriendMsg) {


        XPopup.Builder(context)
            .asConfirm(
                "温馨提示",
                "是否拒绝Ta的添加请求"
            ) {
                refuseRequest(friendMsg)
            }
            .show()

    }

    private fun getDataLoadMoreList() {
        var currentUser = PreferenceUtil.getCurrentUser()
        if (!currentUser.isLogin) {
            return
        }
        lifecycleScope.launch {
            var content = UserService.create()
                .getFriendMsgList(num, size)
            if (content.code == 200) {
                val dataList = content.data
                if (dataList.size > 0) {
                    adapter.addAll(dataList)

                }
            }

        }

    }

    override fun onResume() {
        super.onResume()
//        if (num == 1) {
//
//            getDataFlushList()
//        }
    }


    override fun onDestroyView() {
        super.onDestroyView()
        EventBus.getDefault().unregister(this)
    }

    @Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
    public fun onTabSelectEvent(event: TabSelectEvent) {
        if (event.position == 2) {
            num = 1
            getDataFlushList()

        }

    }


}